<template>
	<view class="main">
		<uni-search-bar @input="search" placeholder="搜索素材名称" bgColor="#fff" />
		<view class="list" v-for="(item,index) in list" :key="index">
			<view class="flex align between info">
				<view class="info-left">
					<view class="title">{{item.name}}</view>
					<view class="time">发布时间：{{item.create_time}}</view>
				</view>
				<!-- <view class="select">选择</view> -->
			</view>
			<view class="flex align between list-btns">
				<view class="list-btn" @click="toDel(item.id)">删除分组</view>
				<view class="list-btn" @click="toEdit(item.name,item.id)">编辑分组</view>
				<navigator :url="'/pages/manage/materialList?id='+item.id" hover-class="none" class="list-btn">素材管理
				</navigator>
			</view>
		</view>
		<uni-load-more :status="status" v-if="!empty" :content-text="loadText"></uni-load-more>
		<xw-empty :isShow="empty" text="暂无数据" textColor="#777777"></xw-empty>
		<view class="btns" @click="show=true">新建分组</view>
		<!-- 添加分组 -->
		<view class="show" v-if="show">
			<view class="showCon">
				<view class="show-tit">新建分组</view>
				<input placeholder="输入分组名称" v-model="name" />
				<view class="flex align between show-btns">
					<view class="show-btn show-btn2" @click="show=false">取消</view>
					<view class="show-btn" @click="toAdd">立即添加</view>
				</view>
			</view>
		</view>
		<view class="show" v-if="show2">
			<view class="showCon">
				<view class="show-tit">编辑分组</view>
				<input placeholder="输入分组名称" v-model="name2" />
				<view class="flex align between show-btns">
					<view class="show-btn show-btn2" @click="show2=false">取消</view>
					<view class="show-btn" @click="toEditCon">确认修改</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		materialLists,
		materialGroupAdd,
		materialGroupEdit,
		materialGroupDelete
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				show: false,
				show2: false,
				totalPage: 1,
				page: 0,
				offset: 10,
				list: [],
				scrollTop: 0,
				empty: false,
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				status: 'loadmore',
				name: '',
				name2: '',
				gId: '',
				delId:''
			};
		},
		onLoad() {
			this.getlist()
		},
		onPageScroll(e) {
			const that = this;
			that.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			const that = this;
			that.getlist();
		},
		methods: {
			toDel(id){
				let that = this
				that.delId = id
				uni.showModal({
					title: "提示",
					content: "确定要删除该分组吗？",
					showCancel: true,
					success: res => {
						if (res.confirm) {
							that.delCon()
						} else {
						
						}
					}
				
				})
			},
			async delCon(){
				const result = await materialGroupDelete({
					ids: this.delId
				})
				if (result.code == 1) {
					uni.showToast({
						title: result.msg,
					})
					setTimeout(()=>{
						this.page = 0
						this.list = []
						this.getlist()
					},500)
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'error'
					})
				}
			},
			toEdit(name, id) {
				this.show2 = true
				this.name2 = name
				this.gId = id
			},
			async toEditCon() {
				this.show2 = false
				const result = await materialGroupEdit({
					name: this.name2,
					id: this.gId
				})
				if (result.code == 1) {
					uni.showToast({
						title: result.msg,
					})
					setTimeout(()=>{
						this.page = 0
						this.list = []
						this.getlist()
					},500)
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'error'
					})
				}
			},
			async toAdd() {
				this.show = false
				const result = await materialGroupAdd({
					name: this.name
				})
				if (result.code == 1) {
					uni.showToast({
						title: result.msg,
					})
					setTimeout(()=>{
						this.page = 0
						this.list = []
						this.getlist()
					},500)
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'error'
					})
				}
			},
			async getlist() {
				const that = this;
				const {
					totalPage,
					page,
					offset,
					list,
				} = that;
				let obj = {
					size: offset,
					page: page + 1,
					// channel_id: channel_id
				};
				that.status = 'loading';
				if (totalPage > page) {
					let result = await materialLists(obj);
					if (result.code == 1) {
						that.page = page + 1;
						if (result.data.total == 0) {
							console.log((888))
							that.empty = true;
							that.status = 'nomore';
							return;
						}
						that.empty = false;
						setTimeout(() => {
							that.status = 'loadmore';
						}, 500);
						that.list = list.concat(result.data.list);
						that.totalPage = Math.ceil(result.data.total / offset);
					}
				} else {
					setTimeout(() => {
						that.status = 'nomore';
					}, 500);
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		padding-bottom: 150rpx;

		.show {
			position: fixed;
			width: 100%;
			height: 100%;
			left: 0;
			top: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			background: rgba(0, 0, 0, 0.3);

			.showCon {
				background: white;
				text-align: center;
				width: 76%;
				padding: 37rpx;

				.show-tit {
					font-size: 38rpx;
					font-weight: bold;
					margin-bottom: 25rpx;
				}

				input {
					border: solid 1rpx #707070;
					padding: 17rpx;
					width: 80%;
					display: block;
					margin: 0 auto 50rpx;
				}

				.show-btn {
					background: #FF6543;
					border-radius: 12rpx;
					width: 188rpx;
					line-height: 60rpx;
					color: white;
					text-align: center;
					font-size: 32rpx;
					font-weight: bold;
					margin: 0 auto;
				}

				.show-btn2 {
					background: gainsboro;
				}
			}
		}
	}

	.list {
		margin: 20rpx 30rpx;
		background: white;
		border-radius: 12rpx;
		box-shadow: 1rpx 1rpx 5rpx 1rpx gainsboro;
		padding: 20rpx;

		.info {
			padding-bottom: 24rpx;
			border-bottom: solid 1rpx gainsboro;

			.info-left {
				.title {
					font-size: 36rpx;
					font-weight: bold;
					margin-bottom: 24rpx;
				}

				.time {
					color: #AAAAAA;
					font-size: 26rpx;
				}
			}

			.select {
				color: white;
				background: #FF6543;
				border-radius: 12rpx;
				font-size: 28rpx;
				font-weight: bold;
				width: 144rpx;
				line-height: 63rpx;
				text-align: center;
			}
		}

		.list-btns {
			justify-content: space-around;
			padding-top: 16rpx;

			.list-btn {
				text-align: center;
				padding: 10rpx 20rpx;
				color: #FF6543;
				font-size: 28rpx;
				border: solid 1rpx;
				border-radius: 10rpx;
			}
		}


	}

	.btns {
		background: #FF6543;
		width: 100%;
		line-height: 108rpx;
		font-size: 34rpx;
		font-weight: bold;
		text-align: center;
		bottom: 0;
		left: 0;
		color: white;
		position: fixed;
	}
</style>